import java.awt.Point;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;

import dvonn.ai.Solver;
import dvonn.game.GameField;
import dvonn.game.Move;
import dvonn.game.Token;


public class dvonn {

	private final Random r = new Random(new Date().getTime());
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GameField game = new GameField();
		dvonn d = new dvonn();
		for (int x=0; x<game.getWidth();x++) {
			for (int y=0; y<game.getHeight();y++) {
				if (game.getToken(x, y)!=Token.RESERVED) {
					game.setToken(new Point(x,y), d.getRandomToken() );
				}
			}
		}
		game.checkAllMovable();
		
		Token t = Token.WHITE;
		Token[] tokens = {Token.WHITE,Token.BLACK};
		GameField testGame;
		Solver s;
		Move m;
		System.out.println(game);
		
		for (int iter=0;iter<50;iter++) {
			System.out.println("Zug Nummer : "+iter);
			if (t==Token.BLACK) {
				ArrayList<Point> liste = game.getAllMovable(t);
				System.out.println(liste.size()+" movable "+t+" token");
				ArrayList<Point> liste2 = new ArrayList<Point>();
				while (liste2.isEmpty()&&!liste.isEmpty()) {
					liste2 = game.getAllValidTargets(liste.get(0));
					System.out.println(liste2.size()+" possible moves from "+pPrint(liste.get(0)));
					if (!liste2.isEmpty()) {
						System.out.println(pPrint(liste.get(0))+"->"+pPrint(liste2.get(0)));
						game.doMove(liste.get(0), liste2.get(0));
					}
					liste.remove(0);
				}
			} else {
				testGame = new GameField(game);
				s = new Solver();
				m = s.getMove(testGame,t, 100000);
				System.out.println("do move : "+m);
				if (m!=null) game.doMove(m.getSource(), m.getTarget());
			}
			System.out.println(game);
			int[] score = game.getScore(tokens);
			for (int i=0; i<score.length; i++) {
				System.out.print(tokens[i]+"="+score[i]+" : ");
			}
			System.out.print("\n");
			game.dvonnChecker();
/*			System.out.println(game);
			score = game.getScore(tokens);
			for (int i=0; i<score.length; i++) {
				System.out.print(tokens[i]+"="+score[i]+" : ");
			}*/
			System.out.println();
			if (t == Token.WHITE) {
				t = Token.BLACK;
			} else {
				t = Token.WHITE;
			}
		}
		
	}

	public Token getRandomToken() {
		final Integer ret = r.nextInt()%98;
		if (ret<-10) return Token.BLACK;
		if (ret>10) return Token.WHITE;
		return Token.DVONN;
	}
	
	public static String pPrint(Point p) {
		return "(x="+p.x+",y="+p.y+")";
	}
	
}
